package org.jfk.core.utils.e189id;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * 天翼账号开放平台相关算法
 * @author jiangxf
 *
 */
public class HMACSHA1 {

	private static final String HMAC_SHA1 = "HmacSHA1";

	/**
	 * 生成签名数据
	 * 
	 * @param data 待加密的数据
	 * @param key 加密使用的key
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 */
	public static String getSignature( String data, String key ) throws Exception {
		byte[] keyBytes = key.getBytes();
		SecretKeySpec signingKey = new SecretKeySpec( keyBytes, HMAC_SHA1 );
		Mac mac = Mac.getInstance( HMAC_SHA1 );
		mac.init( signingKey );
		byte[] rawHmac = mac.doFinal( data.getBytes() );
		StringBuilder sb = new StringBuilder();
		for( byte b : rawHmac ) {
			sb.append( byteToHexString( b ) );
		}
		return sb.toString();
	}

	private static String byteToHexString( byte ib ) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		char[] ob = new char[2];
		ob[0] = Digit[(ib >>> 4) & 0X0f];
		ob[1] = Digit[ib & 0X0F];
		String s = new String( ob );
		return s;
	}
	
	public static void main(String[] args) throws Exception{
 
		String appSecret = "LzJRek8pMWR5ddNbs5cvFuAZx6SAwUMl";
		
		String timeStamp = ""+System.currentTimeMillis();
		
		String returnURL = "http://202.103.63.44:5502/jfk";
		String plainText = "timeStamp="+timeStamp+"&returnURL="+returnURL;
		String cipherText = 
				XXTea.encrypt(plainText,"UTF-8",ByteFormat.toHex(appSecret.getBytes()));		
		System.out.println(cipherText);

		String appId = "8134110303";
		String clientType = "20100";
		String format = "redirect";
		String version = "v1.1";
		String paras = cipherText;

		String pText = XXTea.decrypt("5107F79D7C21BBEB1BBF78C35BF7AC5F0ABF2CDB2B3FC6DEEF74909ECE14A7486028E7456645C661704D5C6AE29856D678943D49BE00B059AD10BA69E6F1C19C8A5B8772CCD9D41D01F13325F27675962117D0C9769729D599137328508F4B866D58F88275346A03EDD8D5777B893B19A4297707F004819DEFD351A3A78EE53F1E622E36D2BF39B42F24CCE40D9D9D6865EB203E68836EBD0B294AEFEE19F85A6373E7DCF73697B9DFE4F0003630B75CB80BF3BEB898699273C8496F0A1A826916D2B20D6AC4AF1520A06B9DE526F7E359F2AD23A2D199D7B535F6C761CD7952EEE2C363F477D0B26FD0A843DC28106A99AEE8F7C90AF2F2A83057F655B5702A09AF30E7C58F7939A075FA622A69352F7E3CA524D6C73165EE1C534D8038D1FD8EC1F969B978D6BDB766A04555DC165997D61B1C1E5F9D3A1417B8F4","UTF-8",ByteFormat.toHex(appSecret.getBytes()));
		System.out.println(pText);		
		
		String plainSig = appId+clientType+format+version+paras;
		String sign = HMACSHA1.getSignature(plainSig,appSecret).toUpperCase();
		System.out.println("sign:"+ sign);
		
		String authUrl = "http://open.e.189.cn/api/logbox/oauth2/autoLogin.do";
		
		authUrl += "?appId="+appId
				+ "&clientType="+clientType
				+ "&format="+format
				+ "&version="+version
				+ "&paras="+paras
				+ "&sign="+sign
				+ "&timeStamp="+timeStamp
				+ "&returnURL="+returnURL; 
		System.out.println(authUrl);
	}
}
